/*
    This file is part of Slydini.

    Slydini is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Slydini is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Slydini.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "right_menu.h"
#include <QtGui>
#include <stdio.h>

#include "moc_rightmenu.h"

RightMenu::RightMenu(QWidget *parent):Right_form(){
	setupUi(this);
    connect(toolButtonRU, SIGNAL(clicked()), this, SLOT(changeImagesUp()));
	connect(toolButtonRD, SIGNAL(clicked()), this, SLOT(changeImagesDown()));
	connect(toolButtonN, SIGNAL(clicked()), this, SLOT(EmptyImage()));
	connect(toolButtonC, SIGNAL(clicked()), this, SLOT(CameraImage()));
    connect(toolButtonP1, SIGNAL(clicked()), this, SLOT(loadImage1()));
    connect(toolButtonP2, SIGNAL(clicked()), this, SLOT(loadImage2()));
    connect(toolButtonP3, SIGNAL(clicked()), this, SLOT(loadImage3()));
    w_image = NULL;
}

RightMenu::~RightMenu(){}

QImage *RightMenu::ChoosenPicture() {

       return w_image;                 
       }

void RightMenu::changeImagesUp(){
        QIcon icon1;
        QIcon icon2;
        QIcon icon3;
        
        QDir dir(QString("gui/images2/"), "*.jpg");
        
        if (dir.count() > 2){
        int i ;

            if ( (last_image-5) < 0 ){
                 i=last_image+dir.count()-5;
            }
            else {
                 i= last_image-5;
            }
            icon1.addPixmap(QPixmap(QString::fromUtf8("gui/images2/") + dir[i]), QIcon::Normal, QIcon::Off);
            
            if ( (last_image-4) < 0 ){
                 i=last_image+dir.count()-4;
            }
            else {
                 i= last_image-4;
            }
            icon2.addPixmap(QPixmap(QString::fromUtf8("gui/images2/") + dir[i]), QIcon::Normal, QIcon::Off);
            
            if ( (last_image-3) < 0 ){
                 i=last_image+dir.count()-3;
                 
                 last_image = i;
            }
            else {
                 i= last_image-3;
                 last_image = i;
            }
            icon3.addPixmap(QPixmap(QString::fromUtf8("gui/images2/") + dir[i]), QIcon::Normal, QIcon::Off);                   

        toolButtonP1->setIcon(icon1);
        toolButtonP2->setIcon(icon2);
        toolButtonP3->setIcon(icon3);
        }        


}

void RightMenu::changeImagesDown(){
        QIcon icon1;
        QIcon icon2;
        QIcon icon3;
        
        QDir dir(QString("gui/images2/"), "*.jpg");
        
        if (dir.count() > 2){
        
        unsigned int i = last_image+1;

            if ( (unsigned)last_image+1 >= dir.count() ){
                 i=last_image+1-dir.count();
            }
            else {
                 i= last_image+1;
            }
            icon1.addPixmap(QPixmap(QString::fromUtf8("gui/images2/") + dir[i]), QIcon::Normal, QIcon::Off);
            
            if ( (unsigned)last_image+2 >= dir.count() ){
                 i=last_image+2-dir.count();
            }
            else {
                 i= last_image+2;
            }
            icon2.addPixmap(QPixmap(QString::fromUtf8("gui/images2/") + dir[i]), QIcon::Normal, QIcon::Off);
            
            if ( (unsigned)last_image+3 >= dir.count() ){
                 i=last_image+3-dir.count();
                 last_image = i;
                 printf("%d\n",i);
            }
            else {
                 i= last_image+3;
                 last_image = i;
                 printf("%d\n",i);
            }
            icon3.addPixmap(QPixmap(QString::fromUtf8("gui/images2/") + dir[i]), QIcon::Normal, QIcon::Off);                   

        toolButtonP1->setIcon(icon1);
        toolButtonP2->setIcon(icon2);
        toolButtonP3->setIcon(icon3);
        }     
     }

void RightMenu::UseImage(){}

void RightMenu::EmptyImage(){}

void RightMenu::CameraImage(){
    w_image = NULL;
}

void RightMenu::loadImage1(){
	c_image= last_image-2;
	printf("%d",c_image);   
          QDir dir(QString("gui/images2/"), "*.jpg");
          if (w_image != NULL)
               delete w_image;
          w_image = new QImage(QString::fromUtf8("gui/images2/") + dir[c_image]);        
}
void RightMenu::loadImage2() {
	c_image= last_image-1;
	printf("%d",c_image);
          QDir dir(QString("gui/images2/"), "*.jpg");
          if (w_image != NULL)
               delete w_image;
          w_image = new QImage(QString::fromUtf8("gui/images2/") + dir[c_image]);        
}
void RightMenu::loadImage3(){
	c_image= last_image;
	printf("%d",c_image); 
          QDir dir(QString("gui/images2/"), "*.jpg");
          if (w_image != NULL)
               delete w_image;
          w_image = new QImage(QString::fromUtf8("gui/images2/") + dir[c_image]);        
}
